<?php
function renderResult(){

}
function ajaxReturn($data){
    echo json_encode($data);
    exit;
}
function returnResult($errcode=-1,$errmsg='fail'){
    $data=array();
    $data['errcode']=$errcode;
    $data['errmsg']=$errmsg;
    echo json_encode($data);
    exit;
}
/**
 * 判断接收过来的数据是否为空
 */
function fileterEmpty($data,$errmsg){
    if(empty($data)){
        returnResult(200001,$errmsg);
    }else{
        return trim($data);
    }
}
/**
 * 获取登录地址
 */
function getAddressInfo($ip) {
    $ip='58.23.232.201';
    //$ip='127.0.0.1';
    $ch = curl_init("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$ip}");
    //curl_setopt($ch,CURLOPT_ENCODING ,'utf8');
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
    $location = json_decode(curl_exec($ch));

    curl_close($ch);
    $dataArray=Array();
   //$location=json_decode($location);

   /* if ($location['ret'] == -1) {
        return [
            'status' => 0,
            'info' => '获取地理位置失败'
        ];
    }*/
    if(is_object($location)){

        $dataArray['province']=$location->province;
        $dataArray['city']=$location->city;
    }else if($location==-2){
        $dataArray['province']='未知';
        $dataArray['city']='未知';

    }
    return $dataArray;

}
/**
 * 获取真实ip
 */
function get_real_ip(){
    $ip=false;
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        $ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
        if($ip){ array_unshift($ips, $ip); $ip=FALSE; }
        for ($i=0; $i < count($ips); $i++){
            if(!eregi ('^(10│172.16│192.168).', $ips[$i])){
                $ip=$ips[$i];
                break;
            }
        }
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
/**
 * 字段为空的字段
 */
function getData($value,$errmsg){
    if(empty($value)){
        returnResult(400001,$errmsg);
    }else{
        return $value;
    }

}
/**
 * 获取当前管理员的Id
 */
function getUserInfo(){
    $userInfo=Yii::$app->session->get('userInfo');
    return $userInfo;
}
/**
 * 获取管理员的当前Id
 */
function getUserId(){
    $userInfo=Yii::$app->session->get('userInfo');
    return $userInfo['id'];
}
/*
 * 判断是否有权限没权限输出提示
 */
function checkGrant($grant){
    $userInfo=Yii::$app->session->get('userInfo');
    $check=strpos($userInfo['roles']['granted'],$grant);

    if(!$check){
        returnResult(200001,'没有权限');
    }

}
/**
 * 视图层判断是否显示隐藏
 */
function viewGrant($grant){
    $userInfo=Yii::$app->session->get('userInfo');
    $check=strpos($userInfo['roles']['granted'],$grant);

    if(!$check){
        return false;
    }else{
        return true;
    }
}

